package com.itheima.crops.controller;

import com.github.pagehelper.Page;
import com.itheima.crops.pojo.*;
import com.itheima.crops.service.IndexService;
import com.itheima.crops.service.PcropsService;
import com.itheima.entity.PageResult;
import com.itheima.entity.Result;
import com.itheima.entity.StatusCode;
import jdk.nashorn.internal.ir.JumpToInlinedFinally;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/pcrops")
public class PcropsController {

    @Autowired
    private PcropsService pcropsService;

    /***
     * 分页搜索实现
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    @GetMapping(value = "/search/{page}/{size}" )
    public Result findPage(@RequestParam Map searchMap, @PathVariable int page, @PathVariable  int size){
        try {
            Page<Pcrops> pageList = pcropsService.findPage(searchMap, page, size);
            PageResult pageResult=new PageResult(pageList.getTotal(),pageList.getResult());
            return new Result(true, StatusCode.OK,"查询成功！",pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,StatusCode.ERROR,"查询失败！");
        }
    }

    /**
     * 农作物全查
     * @return
     */
    @GetMapping("/findAll")
    public Result findAll() {
        try {
            List<Pcrops> pcropsList = pcropsService.findAll();
            return new Result(true, StatusCode.OK,"查询农作物成功！",pcropsList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询农作物失败！");
        }
    }

    /**
     * 农作物根据id查询并查询关联表
     * @param id
     * @return
     */
    @GetMapping("/findById/{id}")
    public Result findById(@PathVariable("id") Integer id) {
        try {
            Crops crops = pcropsService.findById(id);
            return new Result(true,StatusCode.OK,"根据id查询农作物成功！",crops);
        } catch (Exception e) {
            e.printStackTrace();
            if("农作物不存在".equals(e.getMessage())) {
                return new Result(false,StatusCode.NOTEXIST,"根据id查询农作物失败，农作物不存在！");
            }
            return new Result(false,StatusCode.OK,"根据id查询农作物失败！");
        }
    }

    /**
     * 农作物根据id查询基本信息
     * @param id
     * @return
     */
    @GetMapping("/findById1/{id}")
    public Result findById1(@PathVariable("id") Integer id) {
        try {
            Pcrops pcrops = pcropsService.findById1(id);
            return new Result(true,StatusCode.OK,"根据id查询农作物成功！",pcrops);
        } catch (Exception e) {
            e.printStackTrace();
            if("农作物不存在".equals(e.getMessage())) {
                return new Result(false,StatusCode.NOTEXIST,"根据id查询农作物失败，农作物不存在！");
            }
            return new Result(false,StatusCode.OK,"根据id查询农作物失败！");
        }

    }

    /**
     * 查农作物与指标关联关系
     * @param id 农作物id
     * @return
     */
    @GetMapping("/pcropsIndexInfoList/{id}")
    public Result pcropsIndexInfoList(@PathVariable("id") Integer id) {
        try {
            List<PcropsIndexInfo> pcropsIndexInfos = pcropsService.pcropsIndexInfoList(id);
            return new Result(true,StatusCode.OK,"查询农作物与指标关联成功！",pcropsIndexInfos);
        } catch (Exception e) {
            e.printStackTrace();
            if("农作物不存在".equals(e.getMessage())) {
                return new Result(false,StatusCode.NOTEXIST,"查询农作物与指标关联关系失败，农作物不存在！");
            }
            return new Result(false,StatusCode.OK,"查询农作物与指标关联失败！");
        }
    }

    /**
     * 查询农作物与病虫害关联关系
     * @param id 农作物id
     * @return
     */
    @GetMapping("/diseaseList/{id}")
    public Result diseaseList(@PathVariable("id") Integer id) {
        try {
            List<Disease> diseases = pcropsService.diseaseList(id);
            return new Result(true,StatusCode.OK,"查询农作物与病虫害关联成功！",diseases);
        } catch (Exception e) {
            e.printStackTrace();
            if("农作物不存在".equals(e.getMessage())) {
                return new Result(false,StatusCode.NOTEXIST,"查询农作物与病虫害关联关系失败，农作物不存在！");
            }
            return new Result(false,StatusCode.OK,"查询农作物与病虫害关联失败！");
        }
    }

   //新增农作物基本信息并返回农作物id
    @PostMapping("/add")
    public Result add(@RequestBody Pcrops pcrops)  {
        try {
            Integer cid = pcropsService.add(pcrops);
            return new Result(true,StatusCode.OK,"新增农作物成功！",cid);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,StatusCode.OK,"新增农作物失败！");
        }
    }

    /**
     * 添加农作物与指标的关联关系
     * @param cid
     * @param pcropsIndexInfoVO
     * @return
     */
    @PostMapping("/addPcropsIndexRel/{cid}")
    public Result addPcropsIndexRel(@PathVariable Long cid,@RequestBody PcropsIndexInfoVO pcropsIndexInfoVO) {
        try {
            pcropsService.addPcropsIndexRel(cid, pcropsIndexInfoVO);
            return new Result(true,StatusCode.OK,"添加农作物与指标的关联关系成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,StatusCode.OK,"添加农作物与指标的关联关系失败！");
        }
    }

    /**
     * 添加农作物与病虫害的关联关系
     * @param cid
     * @param dids
     * @return
     */
    @GetMapping("/addPcropsDiseaseRel/{cid}")
    public Result addPcropsDiseaseRel(@PathVariable Long cid,@RequestParam("dids") Long[] dids) {
        try {
            pcropsService.addPcropsDiseaseRel(cid,dids);
            return new Result(true,StatusCode.OK,"添加农作物与病虫害的关联关系成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,StatusCode.OK,"添加农作物与病虫害的关联关系失败！");
        }
    }

    /**
     * 修改农作物基本信息
     * @param pcrops
     */
    @PostMapping("/update")
    public Result update(@RequestBody Pcrops pcrops) {
        try {
            pcropsService.Update(pcrops);
            return new Result(true,StatusCode.OK,"修改农作物基本信息成功！");
        } catch (Exception e) {
            e.printStackTrace();
            if("农作物不存在".equals(e.getMessage())) {
                return new Result(false,StatusCode.NOTEXIST,"修改失败农作物不存在!");
            }
            return new Result(false,StatusCode.OK,"修改农作物基本信息失败！");
        }
    }

    /**
     * 修改农作物与指标关联关系
     * @param cid
     * @param pcropsIndexInfoVO
     * @return
     */
    @PostMapping("/updatePcropsIndexRel/{cid}")
    public Result updatePcropsIndexRel(@PathVariable("cid") Long cid,@RequestBody PcropsIndexInfoVO pcropsIndexInfoVO) {
        try {
            pcropsService.updatePcropsIndexRel(cid, pcropsIndexInfoVO);
            return new Result(true,StatusCode.OK,"修改农作物与指标的关联关系成功！");
        } catch (Exception e) {
            e.printStackTrace();
            if("农作物不存在".equals(e.getMessage())) {
                return new Result(false,StatusCode.NOTEXIST,"修改失败，农作物不存在!");
            }
            return new Result(false,StatusCode.ERROR,"修改农作物与指标的关联关系失败！");
        }
    }

    /**
     * 修改农作物与病虫害关联关系
     * @param cid
     * @param dids
     * @return
     */
    @GetMapping("/updatePcropsDiseaseRel/{cid}")
    public Result updatePcropsDiseaseRel(@PathVariable("cid") Long cid,@RequestParam("dids") Long[] dids) {
        try {
            pcropsService.updatePcropsDiseaseRel(cid, dids);
            return new Result(true,StatusCode.OK,"修改农作物与病虫害关联关系成功！");
        } catch (Exception e) {
            e.printStackTrace();
            if("农作物不存在".equals(e.getMessage())) {
                return new Result(false,StatusCode.NOTEXIST,"修改失败，农作物不存在!");
            }
            return new Result(false,StatusCode.ERROR,"修改农作物与病虫害关联关系失败！");
        }
    }

    /**
     * 删除农作物以及农作物与农作物指标关联关系，农作物与病虫害关联关系
     * @param id
     * @return
     */
    @GetMapping("/delete/{id}")
    public Result delete(@PathVariable("id") Long id) {
        try {
            pcropsService.delete(id);
            return new Result(true,StatusCode.OK,"删除农作物成功！");
        } catch (Exception e) {
            e.printStackTrace();
            if("农作物不存在".equals(e.getMessage())) {
                return new Result(false,StatusCode.NOTEXIST,"删除失败，农作物不存在!");
            }
            if("该农作物已种植".equals(e.getMessage())) {
                return new Result(false,StatusCode.EXISTREL,"删除失败，该农作物已种植不能删除!");
            }
            return new Result(false,StatusCode.ERROR,"删除农作物失败！");
        }
    }
}
